package com.lp.mgt.configs;

import java.util.List;
import java.util.Properties;

import javax.annotation.PostConstruct;

import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

import com.github.pagehelper.PageInterceptor;
import com.lp.mgt.configs.mybatis_intecepter.DataPremissionInterceptor;


@Configuration
public class AppConfiguration{

    @Autowired
    private RestTemplateBuilder builder;

    @Bean
    public RestTemplate restTemplate() {
        return builder.build();
    }
    
    @Autowired
    private List<SqlSessionFactory> sqlSessionFactoryList;
    @Bean
    @ConfigurationProperties(prefix = "pagehelper")
    public Properties pageHelperProperties() {
        return new Properties();
    }
 
   //让mybatis数据权限拦截器在分页插件拦截器之前拦截 
    @PostConstruct
    public void mybatisCc() {
    	DataPremissionInterceptor dataPermissionInterceptor = new DataPremissionInterceptor();//自动义数据权限拦截器
    	//分页拦截器
    	PageInterceptor  pageInterceptor = new PageInterceptor();
        pageInterceptor.setProperties(this.pageHelperProperties());
        for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
        	sqlSessionFactory.getConfiguration().addInterceptor(pageInterceptor);
        	sqlSessionFactory.getConfiguration().addInterceptor(dataPermissionInterceptor);
        }
    }
}
